package com.gitee.wsl.ext.base


import com.gitee.wsl.struct.attribute.Attribute
import com.gitee.wsl.struct.attribute.AttributeSet
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import org.junit.Test


class AttributeTest {

    @Test
    fun testAttribute(){
        val attribute=Attribute(id= 1, value = "test_attribute")
        val attributeValue=attribute.getDefaultAttributeValue()

        println("attribute: -=> ${toJson(attribute)}")
        println("attributeValue: -=> ${toJson(attributeValue)}")
        println()

        val listAttribute=Attribute(id= 99, value = "test_list_attribute", isListValue = true)
        val listAttributeValue=listAttribute.getDefaultAttributeValue()

        println("listAttribute: -=> ${toJson(listAttribute)}")
        println("listAttributeValue: -=> ${toJson(listAttributeValue)}")
        println()

        val attributeSet=AttributeSet(id=66, mutableListOf(attribute,listAttribute))
        val attributeSetValue=attributeSet.getDefaultAttributeSetValue()

        println("attributeSet: -=> ${toJson(attributeSet)}")
        println("attributeSetValue: -=> ${toJson(attributeSetValue)}")
        println()

        attributeSetValue.getAttributeValueList().forEachIndexed { index, abstractAttributeValue ->
            println("attributeSetValueList:$index: -=> ${toJson(abstractAttributeValue)}")
        }

    }

    val defaultMoshiBuilder: Moshi.Builder by lazy{
        Moshi.Builder().addLast(KotlinJsonAdapterFactory())
    }
    val jsonSerializer: Moshi by lazy { defaultMoshiBuilder.build() }

    private fun <T:Any> toJson(value:T):String{
        jsonSerializer.adapter<T>(value::class.java)?.let { jsonAdapter ->
            return jsonAdapter.toJson(value)
        }
        return "null"
    }
}